import re

fix_O_pair_list = [
    (r'I-\w+ O',r"I-","E-"),
    (r'B-\w+ O',r"B-\w+","O"),
    (r'O I-',r"I-","B-"),
    (r'O E-',r"E-","O"),
    (r'B-\w+ B',r" B"," I"),
    (r'E-\w+ E',r"E-","I-")
]

IBstr = r'I-\w+ B-\w+' #fix same B to I not same fix I- to E

def fix(label_str):



    for re_tuple in fix_O_pair_list:
        
        result = re.search(re_tuple[0], string)
        if result:

            match_str = result.group()

            new_str = re.sub( re_tuple[1],re_tuple[2], match_str)

            label_str = label_str.replace(match_str,new_str)

    result = re.search(IBstr, label_str)

    if result:

        match_str = result.group()

        first,second = match_str.split(" ")
        first = first.split("-")[1]
        second = second.split("-")[1]

        if first == second:

            new_str = re.sub( r" B-"," I-", match_str)
        else:

            new_str = re.sub( r"I-","E-", match_str)

        label_str = label_str.replace(match_str,new_str)

    return label_str


